package com.liuxu.topnews;

import android.util.Log;

import com.liuxu.topnews.Adapter.InternetAdapter;

import org.json.JSONException;

import java.io.IOException;
import java.util.Map;

public class Server {
    public  Map topics;
    InternetAdapter database = new InternetAdapter();
    public Server(){
       topics= MyApplication.getTopics();
    }
    public String[] getUpdateInfo(){
        try {
            String responseData=database.getByOkHttp(topics.get("更新").toString());
            String version=database.getjson(responseData,"version");
            String info=database.getjson(responseData,"detial");
            String type=database.getjson(responseData,"force");
            Log.v("性质判断",type);
            String[] updateinfo={version,info,type};
            return updateinfo;
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    public String gettoken(){
        String csrf_token=getdata(topics.get("口令").toString());
        return csrf_token;
    }
    public String[] getideninfo(String action,Map values){
        try {
            String responseData=database.postByOkHttp(topics.get(action).toString(),values);
            String result=topics.get(database.getjson(responseData,"resultcode")).toString();
            String message=database.getjson(responseData,"message");
            if (action.equals("运行")&&result.equals("成功")){
                Log.v("操作为","运行");
                String title=database.getjson(responseData,"title");
                String time=database.getjson(responseData,"time");
                String[] ideninfo={result,message,title,time};
                return ideninfo;
            }else {
                Log.v("操作为",action);
                String[] ideninfo={result,message};
                return ideninfo;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
        Log.v("返回","空");
        return null;
    }

    public String getdata(String name){
        try {
            String data=database.gettoken(name);
            return data;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


}
